package com.wuhongyu.controller;

import com.wuhongyu.entity.Refer;
import com.wuhongyu.service.PlanService;
import com.wuhongyu.service.ReferService;
import com.wuhongyu.service.UserService;
import com.wuhongyu.service.impl.PlanServiceImpl;
import com.wuhongyu.service.impl.ReferServiceImpl;
import com.wuhongyu.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 销售咨询表..客户开发计划
 */
@Controller
@RequestMapping("/refer")
public class ReferController {

    @Autowired
    private ReferService referService;
    @Autowired
    private UserService userService;
    @Autowired
    private PlanService planService;

    /**
     * 查找还未分配的人
     * @param model
     * @param request
     * @param refer
     * @param number
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, HttpServletRequest request, Refer refer, Integer number) {
        model.addAttribute("url",MyUtil.getUrl(request));
        model.addAttribute("page",referService.findAll(refer,number));
        return "~sale/list";
    }

    /**
     * 查找已经分配的人
     * @param model
     * @param request
     * @param refer
     * @param number
     * @return
     */
    @RequestMapping("/dev")
    public String dev(Model model, HttpServletRequest request, Refer refer, Integer number) {
        model.addAttribute("url",MyUtil.getUrl(request));
        //向下转型
        model.addAttribute("page",referService.findRefer(refer,number));
        return "~sale/dev";
    }

    /**
     * 分配业务人员之前的数据回显
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/queryDispatch")
    public String queryDispatch(Model model, Integer id){
        model.addAttribute("userList",userService.findAllExit());
        model.addAttribute("refer",referService.findOne(id));
        return "~sale/dispatch";
    }

    /**
     * 分配人员
     * @param refer
     * @return
     */
    @RequestMapping("/dispatch")
    public String dispatch(Refer refer) {
        referService.dispatch(refer);
        return "redirect:/refer/list";
    }


    @RequestMapping("/queryBefore")
    public String queryBefore(Model model, Integer id) {
        model.addAttribute("userList",userService.findAllExit());
        if(id !=null){
            model.addAttribute("refer",referService.findOne(id));
            return "~sale/edit";
        }
        return "~sale/add";
    }
    @RequestMapping("/save")
    public String save(Refer refer) {
        referService.save(refer);
        return "redirect:/refer/list";
    }
    @RequestMapping("/delete")
    public String delete(Integer id) {
        referService.delete(id);
        return "redirect:/refer/list";
    }

    /**
     * 开发成功
     * @param referid
     * @return
     */
    @RequestMapping("/success")
    public String success(Integer referid){
        referService.change(referid,3);
        return "/customer/queryBefore?referid="+referid;
    }

    /**
     *开发失败
     * @param model
     * @param referid
     * @param request
     * @return
     */
    @RequestMapping("/fail")
    public String fail(Model model ,Integer referid,HttpServletRequest request){
        referService.change(referid,2);
        return this.dev(model,request,new Refer(),0);
    }

    /**
     * 查看已归档的客户
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/lookover")
    public String lookover(Model model, Integer id) {
        model.addAttribute("refer",referService.findOne(id));
        model.addAttribute("planList",planService.findByRefer(id));
        return "~sale/dev_detail";
    }


}